package day17_Inet.demo05_4copy;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Scanner;

public class ReceiveCopy {
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        DatagramSocket ds = new DatagramSocket(9999);
        while (true){
            //接收数据
            byte[] bytes = new byte[1024];
            DatagramPacket dp = new DatagramPacket(bytes, 0, bytes.length);
            ds.receive(dp);
            String s = new String(dp.getData(), 0, dp.getLength());
            System.out.println("接受的数据为"+s);
            if (s.equals("bye")){
                break;
            }
            //发送数据
            String s1 = scanner.next();
            byte[] bytes1 = s1.getBytes();
            DatagramPacket dp1 = new DatagramPacket(bytes1, 0, bytes1.length, dp.getAddress(), dp.getPort());
            ds.send(dp1);
            if (s1.equals("bye")){
                break;
            }
        }
        ds.close();
    }
}
